package com.qububu.common.notice;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.qububu.common.notice.dto.NoticeInfoDto;
import com.qububu.common.notice.service.NoticeService;

public class NoticeInterceptor implements HandlerInterceptor {

	@Resource
	private NoticeService noticeService;
	
	// 系统公告的key
	private final String siteNoticeMessageKey = "0";
	// 社区公告的key
	private final String communityNoticeMessageKey = "1"; 
	
	// 需要做处理的Url
	private List<String> needInitUrls;
	
	public List<String> getNeedInitUrls() {
		return needInitUrls;
	}

	public void setNeedInitUrls(List<String> needInitUrls) {
		this.needInitUrls = needInitUrls;
	}

	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub
		
	}

	public boolean preHandle(HttpServletRequest request, HttpServletResponse arg1,
			Object arg2) throws Exception {
		String requestUrl = request.getRequestURI();
		for (String url : needInitUrls) {
			if(requestUrl.endsWith(url)) {
				Map<String, Object> noticeListMap = getNoticeMessagesMap();
				request.setAttribute("noticeListMap", noticeListMap);
				
                return true;
            }
		}
		
		return true;
	}
	
	/**
	 * 找到notice信息列表
	 * */
	private Map<String, Object> getNoticeMessagesMap() {
		Map<String, Object> noticeListMap = new HashMap<String, Object>();
		// 从缓存中取出系统公告数据
		/*List<NoticeInfoDto> mainSiteNoticeInfoList = noticeService.getMainNoticeListFromCache(siteNoticeMessageKey);
		List<NoticeInfoDto> mainCommunityNoticeInfoList = noticeService.getMainNoticeListFromCache(communityNoticeMessageKey);
		// 如果是缓存中没有数据则从数据库去并将数据放入缓存
		if ((mainSiteNoticeInfoList == null) && (mainCommunityNoticeInfoList == null) ) {
			// 调用noticeService中的方法,初始化公告数据
			mainSiteNoticeInfoList = noticeService.getMainSiteNoticeList();
			mainCommunityNoticeInfoList = noticeService.getMainCommunityNoticeList();
			//存入缓存
			noticeService.doSaveMainNoticeListToCache(siteNoticeMessageKey, mainSiteNoticeInfoList);
			noticeService.doSaveMainNoticeListToCache(communityNoticeMessageKey, mainCommunityNoticeInfoList);
		}*/
		
		List<NoticeInfoDto> mainSiteNoticeInfoList = noticeService.getMainSiteNoticeList();
		List<NoticeInfoDto> mainCommunityNoticeInfoList = noticeService.getMainCommunityNoticeList();
				
		noticeListMap.put("siteList", mainSiteNoticeInfoList);
		noticeListMap.put("communityList", mainCommunityNoticeInfoList);
		
		return noticeListMap;
	}
	
}
